/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.data.osmt;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * This class is a Helper for database queries. An OsmtQuery encapsulate all
 * data that need for a database query in one object.
 * 
 * @author Johannes
 * 
 */
public class OsmtQuery {

	private String table;
	private String[] columns;
	private String selection;
	private String[] selectionArgs;
	private String groupBy;
	private String having;
	private String orderBy;

	/***
	 * Constructor.
	 * 
	 * @param table The table on which the request is executed.
	 * @param columns The columns that are the result set.
	 * @param selection Arguments, which restrict the query.
	 * @param selectionArgs A list of place-holders, which replaces
	 *            ?s-expressions in the selection.
	 * @param groupBy Rules to group the results.
	 * @param having The SQQL-having clause
	 * @param orderBy How to be order the result set.
	 */
	public OsmtQuery(String table, String[] columns, String selection, String[] selectionArgs,
			String groupBy, String having, String orderBy) {
		init(table, columns, selection, selectionArgs, groupBy, having, orderBy);
	}

	/***
	 * Constructor.
	 * 
	 * @param table The table on which the request is executed.
	 * @param columns The columns that are the result set.
	 * @param selection Arguments, which restrict the query.
	 * @param selectionArgs A list of place-holders, which replaces
	 *            ?s-expressions in the selection.
	 * @param groupBy Rules to group the results.
	 * @param having The SQQL-having clause
	 */
	public OsmtQuery(String table, String[] columns, String selection, String[] selectionArgs,
			String groupBy, String having) {
		init(table, columns, selection, selectionArgs, groupBy, having, null);
	}

	/***
	 * Constructor.
	 * 
	 * @param table The table on which the request is executed.
	 * @param columns The columns that are the result set.
	 * @param selection Arguments, which restrict the query.
	 * @param selectionArgs A list of place-holders, which replaces
	 *            ?s-expressions in the selection.
	 * @param groupBy Rules to group the results.
	 */
	public OsmtQuery(String table, String[] columns, String selection, String[] selectionArgs,
			String groupBy) {
		init(table, columns, selection, selectionArgs, groupBy, null, null);
	}

	/***
	 * Constructor.
	 * 
	 * @param table The table on which the request is executed.
	 * @param columns The columns that are the result set.
	 * @param selection Arguments, which restrict the query.
	 * @param selectionArgs A list of place-holders, which replaces
	 *            ?s-expressions in the selection.
	 */
	public OsmtQuery(String table, String[] columns, String selection, String[] selectionArgs) {
		init(table, columns, selection, selectionArgs, null, null, null);
	}

	/***
	 * Constructor.
	 * 
	 * @param table The table on which the request is executed.
	 * @param columns The columns that are the result set.
	 * @param selection Arguments, which restrict the query.
	 */
	public OsmtQuery(String table, String[] columns, String selection) {
		init(table, columns, selection, null, null, null, null);
	}

	/***
	 * Constructor.
	 * 
	 * @param table The table on which the request is executed.
	 * @param columns The columns that are the result set.
	 */
	public OsmtQuery(String table, String[] columns) {
		init(table, columns, null, null, null, null, null);
	}

	/***
	 * Constructor.
	 * 
	 * @param table The table on which the request is executed.
	 */
	public OsmtQuery(String table) {
		init(table, null, null, null, null, null, null);
	}

	private void init(final String theTable, final String[] theColumns,
			final String theSelection, final String[] theSelectionArgs,
			final String theGroupByClause, final String theHavingClause,
			final String theOrderByClause) {
		this.table = theTable;
		this.columns = theColumns;
		this.selection = theSelection;
		this.selectionArgs = theSelectionArgs;
		this.groupBy = theGroupByClause;
		this.having = theHavingClause;
		this.orderBy = theOrderByClause;
	}

	/**
	 * Performs a query on the given database.
	 * 
	 * @param db The database, the query execute on.
	 * @return whether the execution was successful.
	 */
	Cursor executeOnDatabase(SQLiteDatabase db) {
		return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
	}

	/**
	 * Get the table stored in the query.
	 * 
	 * @return the table name.
	 */
	public String getTable() {
		return table;
	}

	/**
	 * Setter for the table name.
	 * 
	 * @param table to which the query is directed.
	 */
	public void setTable(String table) {
		this.table = table;
	}

	/**
	 * Getter for the columns..
	 * 
	 * @return the columns
	 */
	public String[] getColumns() {
		return columns;
	}

	/**
	 * Setter for the columns.
	 * 
	 * @param columns The columns.
	 */
	public void setColumns(String[] columns) {
		this.columns = columns;
	}

	/**
	 * Getter for the selection.
	 * 
	 * @return the selection
	 */
	public String getSelection() {
		return selection;
	}

	/**
	 * Setter for the selection.
	 * 
	 * @param selection Arguments, which restrict the query.
	 */
	public void setSelection(String selection) {
		this.selection = selection;
	}

	/**
	 * Getter for the selection arguments.
	 * 
	 * @return a list of arguments.
	 */
	public String[] getSelectionArgs() {
		return selectionArgs;
	}

	/**
	 * Setter for selection arguments. Sets a list of place-holders, which
	 * replaces ?s-expressions in the selection.
	 * 
	 * @param selectionArgs The list of arguments.
	 */
	public void setSelectionArgs(String[] selectionArgs) {
		this.selectionArgs = selectionArgs;
	}

	/**
	 * Getter for the group by statement of the sql query.
	 * 
	 * @return The group by clause.
	 */
	public String getGroupBy() {
		return groupBy;
	}

	/**
	 * Setter for the group by clause of the sql statement.
	 * 
	 * @param groupBy The groub by clause.
	 */
	public void setGroupBy(String groupBy) {
		this.groupBy = groupBy;
	}

	/**
	 * Getter for the having clause.
	 * 
	 * @return The having clause.
	 */
	public String getHaving() {
		return having;
	}

	/**
	 * Setter for the having clause.
	 * 
	 * @param having The having clause.
	 */
	public void setHaving(String having) {
		this.having = having;
	}

	/**
	 * Getter for the order by clause.
	 * 
	 * @return The order by clause.
	 */
	public String getOrderBy() {
		return orderBy;
	}

	/**
	 * Setter for the order by clause.
	 * 
	 * @param orderBy The oder by clause.
	 */
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

}
